local yinggong = fk.CreateSkill{
  name = "doublez__yinggong"
}

Fk:loadTranslationTable{
  ["doublez__yinggong"] = "萦宫",
  [":doublez__yinggong"] = "结束阶段，你须弃置一张【杀】，若不能则弃置一张红色牌，若仍不能则失去1点体力并摸一张牌。"
    .."然后视为使用你上一张因此弃置的即时牌，若没有则为【桃】。",
  
  ["#doublez__yinggong"] = "萦宫：你须弃置一张%arg",
  ["doublez__yinggong_slash"] = "【杀】",
  ["doublez__yinggong_red"] = "红色牌",

  ["@$doublez__yinggong_lastImediate"] = "萦宫"
}

yinggong:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yinggong.name) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = table.map(player:getCardIds("he"), Util.Id2CardMapper)
    local slash = table.find(cards, function(card) return card.trueName == "slash" and not player:prohibitDiscard(card) end)
    if slash ~= nil then
      local c = room:askToDiscard(player, {
        max_num = 1,
        min_num = 1,
        cancelable = false,
        skill_name = yinggong.name,
        pattern = "slash",
        prompt = "#doublez__yinggong:::".."doublez__yinggong_slash",
        skip = true,
      })
      event:setCostData(self, {card = c[1], choice = 1})
      return true
    end
    local red = table.find(cards, function(card) return card.color == Card.Red and not player:prohibitDiscard(card) end)
    if red then
      local c = room:askToDiscard(player, {
        max_num = 1,
        min_num = 1,
        cancelable = false,
        skill_name = yinggong.name,
        pattern = ".|.|heart,diomand",
        prompt = "#doublez__yinggong:::".."doublez__yinggong_red",
        skip = true,
      })
      event:setCostData(self, {card = c[1], choice = 1})
      return true
    end
    event:setCostData(self, {choice = 2})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local room = player.room
    if cost_data.choice == 1 then
      room:throwCard(cost_data.card, yinggong.name, player, player)
    else
      room:loseHp(player, 1, yinggong.name)
      if player.dead then return end
      room:drawCards(player, 1, yinggong.name)
    end
    if player.dead then return end
    local card_name = player:getTableMark("@$doublez__yinggong_lastImediate")
    if #card_name == 0 then
      card_name = {"peach"}
    end
    local now_card = Fk:getCardById(cost_data.card)
    local c = Fk:cloneCard(card_name[1])
    c.skillName = yinggong.name
    if not player:canUse(c) then
      if now_card and now_card:isCommonTrick() or now_card.type == Card.TypeBasic then
        room:setPlayerMark(player, "@$doublez__yinggong_lastImediate", {now_card.name})
      end
      return
    end
    room:askToUseVirtualCard(player, {
      name = card_name[1],
      skill_name = yinggong.name,
      cancelable = false
    })
    if player.dead then return end
    if now_card:isCommonTrick() or now_card.type == Card.TypeBasic then
      room:setPlayerMark(player, "@$doublez__yinggong_lastImediate", {now_card.name})
    end
  end
})

return yinggong